package com.winter.main.mapper.master.system;

import com.winter.main.model.dto.NavigationDto;
import com.winter.main.model.entity.SysRule;
import com.winter.main.model.entity.SysUser;
import com.winter.main.utils.MyMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Desc:
 * Author: dushuang
 * Date: Create in 2018/4/18
 */
public interface SysRuleMapper extends MyMapper<SysRule> {

    @Select({"<script>" ,
            "SELECT ",
            "ru.id,",
            "rule_code AS ruleCode,",
            "rule_name AS ruleName,",
            "rule_type AS ruleType,",
            "rule_url AS ruleUrl ",
            "FROM sys_rule ru ",
            "LEFT JOIN sys_role_rule srr ON srr.rule_id = ru.id ",
            "LEFT JOIN sys_user_role sur ON sur.role_id = srr.role_id ",
            "LEFT JOIN sys_user su ON su.id = sur.user_id ",
            "WHERE su.id = #{id} ",
            "AND rule_type = 2 ",
            "GROUP BY ru.id ",
            "</script>"})
    List<SysRule> selectRuleByUser(SysUser user);

    @Select({"<script>" ,
            "SELECT ",
            "ru.id AS targetId,",
            "rule_code AS id,",
            "parent_id AS parentId,",
            "rule_name AS name,",
            "rule_type AS ruleType,",
            "rule_picture AS icon, ",
            "rule_url AS url ",
            "FROM sys_rule ru ",
            "LEFT JOIN sys_role_rule srr ON srr.rule_id = ru.id ",
            "LEFT JOIN sys_user_role sur ON sur.role_id = srr.role_id ",
            "LEFT JOIN sys_user su ON su.id = sur.user_id ",
            "WHERE su.id = #{id} ",
            "AND parent_id = 0 ",
            "AND rule_type = 1 ",
            "GROUP BY ru.id ",
            "</script>"})
    List<NavigationDto> selectRootRule(SysUser user);

    @Select({"<script>" ,
            "SELECT ",
            "ru.id AS targetId,",
            "rule_code AS id,",
            "parent_id AS parentId,",
            "rule_name AS name,",
            "rule_type AS ruleType,",
            "rule_picture AS icon, ",
            "rule_url AS url ",
            "FROM sys_rule ru ",
            "LEFT JOIN sys_role_rule srr ON srr.rule_id = ru.id ",
            "LEFT JOIN sys_user_role sur ON sur.role_id = srr.role_id ",
            "LEFT JOIN sys_user su ON su.id = sur.user_id ",
            "WHERE su.id = #{id} ",
            "AND parent_id != 0 ",
            "AND rule_type = 1 ",
            "GROUP BY ru.id ",
            "</script>"})
    List<NavigationDto> selectChildRuleWithoutRoot(SysUser user);

}
